package com.example.demo.admin.biz.service;

import com.example.demo.admin.common.enums.LockEnum;
import com.example.demo.admin.common.enums.error.BizErrorCodeEnum;
import com.example.demo.admin.common.pojo.request.IntegralRuleRsp;
import com.example.demo.commons.result.common.entity.PageData;
import com.example.demo.commons.result.common.exception.BizException;
import com.example.demo.redis.client.RedissonLockClient;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @Auther: xlq
 * @Date: 2024/4/19
 * @Description:
 */
@Slf4j
public class AdminDemoService {

    @Autowired
    private RedissonLockClient redissonLockClient;

    public PageData<IntegralRuleRsp> listIntegralRule(IntegralRuleRsp integralRuleRsp) {
        RLock rLock = redissonLockClient.getLockAndTry(LockEnum.INTEGRAL_CHANGE_LOCK, integralRuleRsp.toString());
        if (Objects.isNull(rLock)) {
            log.error("积分抵扣，没有获取到锁.userId:{}", 10L);
            throw BizException.create(BizErrorCodeEnum.LOCK_GET_FAIL);
        }
        try {
            List<IntegralRuleRsp> objects = Lists.newArrayList();
            return PageData.create(objects,10L,integralRuleRsp.getPage(),integralRuleRsp.getSize());
        } finally {
            if (rLock.isHeldByCurrentThread()){
                rLock.unlock();
            }
        }
    }
}
